package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CustomizeConfirm1Activity;
import com.hemaapp.yjnh.activity.CustomizeProductionDetailActivity;
import com.hemaapp.yjnh.activity.LoginActivity;
import com.hemaapp.yjnh.bean.CustomBlog;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.math.BigDecimal;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.util.XtomToastUtil;

/**
 * 类描述：ListView版，定制商品列表适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/8 14:44
 * 修改备注：
 */
public class CustomBlogAdapter2 extends BaseAdapter {
    private ArrayList<CustomBlog> datas;
    private Context mContext;

    public CustomBlogAdapter2(Context context, ArrayList<CustomBlog> datas) {
        super(context);
        this.mContext = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public boolean isEmpty() {
        int size = datas == null ? 0 : datas.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_custom_blog_2, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sales)
        TextView tvSales;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_price_old)
        TextView tvPriceOld;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        @Bind(R.id.tv_delivery_date)
        TextView tvDeliveryDate;
        @Bind(R.id.tv_custom)
        TextView tvCustom;
        private int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击进入定制商品详情
                    if (position != -1) {
                        Intent it = new Intent(mContext, CustomizeProductionDetailActivity.class);
                        it.putExtra("blog_id", datas.get(position).getId());
                        mContext.startActivity(it);
                    }
                }
            });
            //2016.10.17 start 屏蔽订单列表中的定制按钮点击操作，因为定制商品要选择附加费用
//            tvCustom.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (position != -1) {
//                        CustomBlog blog = datas.get(position);
//                        User user = BaseApplication.getInstance().getUser();
//                        if (user == null) {
//                            Intent it = new Intent(mContext, LoginActivity.class);
//                            mContext.startActivity(it);
//                        }else {
//                            if (user.getId().equals(blog.getClient_id())) {
//                                XtomToastUtil.showShortToast(mContext, "不能定制自己的商品!");
//                                return;
//                            }
//                            Intent it = new Intent(mContext, CustomizeConfirm1Activity.class);
//                            it.putExtra("blog_id", blog.getId());
//                            mContext.startActivity(it);
//                        }
//                    }
//                }
//            });
            //2016.10.17 end 屏蔽订单列表中的定制按钮点击操作，因为定制商品要选择附加费用
        }

        public void setDatas(int position) {
            this.position = position;
            CustomBlog blog = datas.get(position);
            ImageUtils.loadRadiusImage(mContext, datas.get(position).getImgurl(), ivBkg, 10);
            tvName.setText(blog.getName());
            tvSales.setText("已售："+blog.getSellcount());
            tvPrice.setText("¥"+uuUtils.formatAfterDot2(blog.getPrice()));
            tvPriceOld.setText("¥"+uuUtils.formatAfterDot2(blog.getOldprice()));
            tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            tvCash.setText("¥"+uuUtils.formatAfterDot2(price-score+"")+"+");
            tvFoodStamp.setText(uuUtils.formatAfterDot2(blog.getScore())+"粮票");
            tvDeliveryDate.setText("交货时间：" + blog.getSend_date());
        }
    }
}
